The main content of this article is excerpted from Chapters 8 and 10 of the textbook Fundamentals of python Programming, edited by Xueling Zhong and Li Li and published in December 2019 by Electronic Industry Press.
Tushare Financial Data Interface
Installation
The Tushare website is a free and suitable financial data platform for Python developers. The platform can provide financial data covering many categories of data such as China's macro economy, various indices of the domestic stock market, stock trading data of domestic listed companies, regular financial reports of listed companies and domestic financial news. Tushare official website: http://tushare.org
pip install tushare -i https://pypi.tuna.tsinghua.edu.cn/simple#Installation
pip install BeautifulSoup4 -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install openpyxl -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install mplfinance -i https://pypi.tuna.tsinghua.edu.cn/simple
The data returned by Tushare's built-in functions are all of the DataFrame type of Pandas, so it is easier to manipulate the data returned by these functions using the manipulation tools provided by Pandas, NumPy, Matplotlib and other packages.
Platform common interface call testing
Tushare is divided into ordinary interface and pro user interface. The ordinary interface can be used directly without registration, for example, using the ts.get_gdp_year() function of the ordinary interface to obtain Gross Domestic Product (GDP) data.
import tushare as ts
df=ts.get_gdp_year()
df.head()
Return Value Description:
Parameter | Explanation |
---|---|
year | year |
gdp | Gross Domestic Product (billion yuan) |
pc_gdp | Gross domestic product per capita ($) |
gnp | Gross national product (billion yuan) |
pi | Primary sector (billion yuan) |
si | Secondary industry (billion yuan) |
industry | Industry ($bn) |
cons_industry | Construction (billions of yuan) |
ti | Tertiary sector (billions of yuan) |
trans_industry | Transportation, storage, post and telecommunications (billion yuan) |
lbdy | Wholesale, retail trade and restaurants (billion yuan) |
Registering the pro interface
Tushare's normal interface can be used directly without registration, but less data, the more advanced pro interface requires the user to register with the platform https://tushare.pro/register and set up user credential information in the runtime environment before before the pro interface can be used and more data can be downloaded. The process consists of 7 steps as follows.
(1) Login to the webpage https://tushare.pro/register and register as a Tushare community user.
(2) Login to the Tushare community at https://tushare.pro/login and then perform the following three steps to retrieve the user credentials: First, after logging in successfully, move your mouse to the user name in the top right corner of the page and click on the The user can then access the "User Centre" by clicking on the "Personal Home" option in the drop-down menu. The user will then click on the "Interface TOKEN" tab on the "User Centre" page as follows. Finally, click on the copy icon on the right hand side (circled in red) to copy the entire contents of the text box.
(3) Use the command pip install tushare to install the Tushare package locally.
(4) Execute the import tushare package command import tushare as ts in the IPython operator interface.
(5) Use the built-in function set_token() of the Tushare package to set the token credential information of the local user in the following way, where the credential information should be represented as a string. ts.set_token("user tushare token")
(6) Initialise the pro interface with the command pro = ts.pro_api(). If set_token('user tushare token') is not valid or you do not want to save the token locally, then you can set token: pro_api('user token') directly when initialising the interface.
(7) Data retrieval. After completing the first 6 operations, the user can only call the pro interface function to get the corresponding data.
pro interface call test
The operation of the pro interface function for obtaining daily stock quotes is as follows.
import tushare as ts
pro = ts.pro_api('用户tushare token')
df = pro.daily(ts_code = '600104.SH', start_date = '20000501', end_date = '20200917')
df.head()